//shoppingcarts.js
//获取应用实例
const app = getApp()
const AV = require('../../utils/av-weapp.js')
Page({
  data: {
    num:0,
    total:0,
    carts: [],
    CheckAll:'',
		minusStatuses: [],
		selectedAllStatus: false,
		total: '',
		startX: 0,
		itemLefts: [],
    properties:[],
    cart:false,
    carList:[],
    cartItems:[],
    lo:'https://xiaochengxu.printeasy.top',
    // 'goodList': [
    //   {
    //     'name': '打印机名称',
    //     'author': 'AN-5454541',
    //     'isbn': '9787535482051',
    //     'cover': 'http://china.pr.img.coovee.net/picture/2014-6/18_17_27_56_015.jpg',
    //     'desc': '[黑色打印机+墨水四瓶]',
    //     'price': 88.9,
    //     'count': 1,
    //     'checked': false
    //   }
    // ],
    'checkAll': false,
    'totalCount': 0,
    'totalPrice': 0,
    // 组件所需的参数
    nvabarData: {
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '购物车', //导航栏 中间的标题
    },

    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height * 2 + 20,
  },
  goproduct_deta:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/shoppingcarts/product_details/product_details?id='+id,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  onLoad() {
     // 获取用户信息
     wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
        } else {
          // 未授权，跳转到授权页面
          wx.showModal({
           title:'请登录',
           icon:'none',
            success:function(){
              if (res.confirm) {
                wx.reLaunch({
                  url: '/pages/login/login',
                })
                } else if (res.cancel) {
                  wx.showToast({
                    title: '您拒绝了登陆',
                  })
                }
             
            }
          })
         
        }
      }
    })
    let url = app.globalData.URL+'goods/cart/list';
        let data = {};
        let that = this;
        app.wxRequest('GET', url, data, (res) => {
          let carList = res.rows
          that.setData({
            carList:carList,
            carts:carList,
            cartItems:carList
          })
          console.log(carList)
        }, (err) => {
          console.log(err.errMsg)
        })
  },
//选择
select:function(e){
  var CheckAll = this.data.CheckAll;
  CheckAll = !CheckAll
  var cartItems = this.data.cartItems

  for(var i=0;i<cartItems.length;i++){
    cartItems[i].selected = CheckAll
  }

  this.setData({
    cartItems: cartItems,
    CheckAll: CheckAll
  })
  this.getsumTotal()
 },
 add:function (e) {
   console.log(e)
   var cartItems = this.data.cartItems   //获取购物车列表
   var index = e.currentTarget.dataset.index //获取当前点击事件的下标索引
   var value = cartItems[index].count  //获取购物车里面的value值
   
   value++
   cartItems[index].count = value;
   this.setData({
     cartItems: cartItems
   });
   this.getsumTotal()
   
   wx.setStorageSync("cartItems", cartItems)  //存缓存
 },
 
  //减
 reduce: function (e){
   var cartItems = this.data.cartItems  //获取购物车列表
   var index = e.currentTarget.dataset.index  //获取当前点击事件的下标索引
   var value = cartItems[index].value  //获取购物车里面的value值

  if(value==1){
     value --
     cartItems[index].count = 1
   }else{
     value --
     cartItems[index].count = value;
   }
   this.setData({
     cartItems: cartItems
   });
   this.getsumTotal()
   wx.setStorageSync("cartItems", cartItems)
 },

  // 选择
 selectedCart:function(e){
   
  var cartItems = this.data.cartItems   //获取购物车列表
  var index = e.currentTarget.dataset.index;  //获取当前点击事件的下标索引
  var selected = this.data.cartItems[index].selected;    //获取购物车里面的value值

  //取反
  cartItems[index].selected =! selected;
  this.setData({
    cartItems: cartItems
  })
  this.getsumTotal();   
  wx.setStorageSync("cartItems", cartItems)
 },


 

 //删除
 shanchu:function(e){
   var cartItems = this.data.cartItems  //获取购物车列表
   var index = e.currentTarget.dataset.index  //获取当前点击事件的下标索引
   cartItems.splice(index,1)
   this.setData({
     cartItems: cartItems
   });
   if (cartItems.length) {
     this.setData({
       cartList: false
     });
   }
   this.getsumTotal()
   wx.setStorageSync("cartItems", cartItems)
 },

    //提示
 go:function(e){
   this.setData({
     cartItems:[]
   })
   wx.setStorageSync("cartItems", [])
 },


 //合计
 getsumTotal: function () {
   var sum = 0
   for (var i = 0; i < this.data.cartItems.length; i++) {
     if (this.data.cartItems[i].selected) {
       sum += this.data.cartItems[i].count * this.data.cartItems[i].gcommodityModel.price
     }
   }
   //更新数据
   this.setData({
     total: sum
   })
 },

  // 结算
  bindCheckout(){

  }
})
